package com.shadow.baiqun.delegate.room.change;

import android.device.ScanDevice;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.shadow.baiqun.R;
import com.shadow.baiqun.R2;
import com.shadow.baiqun.delegate.room.adapter.AddCheckTaskAdapter;
import com.shadow.baiqun.delegate.room.adapter.ItemName;
import com.shadow.baiqun.event.RefreshChangeTaskEvent;
import com.shadow.baiqun.event.ScanInfoEvent;
import com.shadow.baiqun.view.TableHeadView;
import com.shadow.core.delegate.RxShadowDelegate;
import com.shadow.core.util.screen.ScreenUtils;
import com.shadow.core.util.toast.ToastUtils;
import com.shadow.ui.recycler.BaseDecoration;
import com.shadow.ui.recycler.MultipleItemEntity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author zhangyu
 * @date 2018/7/13
 * 添加变更
 */

public class ChangeAddTaskDelegate extends RxShadowDelegate {
    public static ChangeAddTaskDelegate create() {
        final ChangeAddTaskDelegate contentDelegate = new ChangeAddTaskDelegate();
        final Bundle bundle = new Bundle();
        contentDelegate.setArguments(bundle);
        return contentDelegate;
    }

    @BindView(R2.id.toolbar)
    Toolbar mToolbar;
    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R2.id.lv_title)
    TableHeadView mHeadView;
    private AddCheckTaskAdapter mAddCheckTaskAdapter;
    private ScanDevice mScanDevice;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mScanDevice = new ScanDevice();
        //广播输出
        mScanDevice.setOutScanMode(0);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_add_check_task;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    pop();
                }
            });
            mToolbar.setTitle("添加变更任务");
        }
        List<String> list = new ArrayList<>();
        list.add("序列号");
        list.add("条码");
        list.add("操作");
        mHeadView.setHeaders(list);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAddCheckTaskAdapter = new AddCheckTaskAdapter(R.layout.item_room_check_add_task, null);
        mRecyclerView.setAdapter(mAddCheckTaskAdapter);
        mRecyclerView.addItemDecoration(BaseDecoration.create(getContext().getResources().getColor(R.color.gray_light), 1));
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {

            }

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (R.id.btn_delete == view.getId()) {
                    adapter.remove(position);
                }
            }
        });
    }

    @Override
    public boolean onBackPressedSupport() {
        pop();
        return false;
    }

    @Override
    public void onResume() {
        super.onResume();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void scanEvent(ScanInfoEvent event) {
        int number = mAddCheckTaskAdapter.getItemCount();
        mAddCheckTaskAdapter.addData(number, MultipleItemEntity.builder()
                .setField(ItemName.ITEM_SERIALIZE_NUMBER, number + 1)
                .setField(ItemName.ITEM_BORAD_CODE, event.mInfo)
                .build());
    }

    @OnClick(R2.id.tv_scan_add)
    public void onScanAdd() {
        if (!mScanDevice.isScanOpened()) {
            mScanDevice.openScan();
        }
        mScanDevice.startScan();
    }

    @OnClick(R2.id.tv_commit)
    public void onCommit() {
        List<MultipleItemEntity> list = mAddCheckTaskAdapter.getData();
        if (!list.isEmpty()) {
            DialogReason dialogReason = new DialogReason(_mActivity, analysisList(list), new DialogReason.OnPostDataListener() {
                @Override
                public void onSucceed() {
                    ToastUtils.showToast(getContext(), "添加成功");
                    EventBus.getDefault().post(new RefreshChangeTaskEvent());
                }

                @Override
                public void onError() {
                    ToastUtils.showToast(getContext(), "添加失败");
                }
            });
            Window dialogWindow = dialogReason.getWindow();
            if (dialogWindow != null) {
                WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                lp.width = ScreenUtils.getScreenWidth() * 4 / 5; // 宽度
                lp.height = ScreenUtils.getScreenHeight() / 2; // 高度
                dialogWindow.setGravity(Gravity.CENTER);
                dialogWindow.setAttributes(lp);
                dialogReason.show();
            }
        } else {
            ToastUtils.showToast(getContext(), "请添加数据!");
        }
    }

    private String analysisList(List<MultipleItemEntity> list) {
        StringBuilder builder = new StringBuilder();
        for (MultipleItemEntity multipleItemEntity : list) {
            builder.append(multipleItemEntity.getField(ItemName.ITEM_BORAD_CODE)).append(",");
        }
        String info = builder.toString();

        return info.substring(0, info.length() - 1);
    }
}
